package com.zzty.train.util;

import java.io.Serializable;
import java.util.List;

public class PageUtils implements Serializable {
    private static final long serialVersionUID = -5247614532234782640L;
    public final static String PAGE = "page";
    public final static String PAGE_NO = "pageno";
    public final static String PAGE_SIZE = "pagesize";

    private long pageSize;//每页显示记录数
    private long firstResult=0;//当页第一条记录号
    private long totalCount;//总记录数
    private long totalPage;//总页码
    private long pageNo;//当前页码
    private List<?> sumData;//此集合可用来保存 合计数据
    private List<?> data;//查询结果

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }
    public long getFirstResult() {
        if(pageNo>0){
            firstResult=pageSize * (pageNo -1);
        }else{
            firstResult = 0;
        }
        return firstResult;
    }

    public long getNextPageResult(){
        if(pageNo>0){
            return pageSize*(pageNo-1);
        }else{
            return pageNo;
        }
    }

    public void setFirstResult(long firstResult) {
        this.firstResult = firstResult;
    }
    public long getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
        totalPage = this.totalCount/pageSize;
        if (totalPage == 0 || totalCount % pageSize != 0) {
            totalPage++;
        }
    }
    public long getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }
    public long getPageNo() {
        return pageNo;
    }
    public void setPageNo(long pageNo) {
        this.pageNo = pageNo;
    }

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }

    /**
     * 是否第一页
     */
    public boolean isFirstPage() {
        return pageNo <= 1;
    }

    /**
     * 是否最后一页
     */
    public boolean isLastPage() {
        return pageNo >= getTotalPage();
    }

    /**
     * 下一页页码
     */
    public long getNextPage() {
        if (isLastPage()) {
            return pageNo;
        } else {
            return pageNo + 1;
        }
    }

    /**
     * 上一页页码
     */
    public long getPrePage() {
        if (isFirstPage()) {
            return pageNo;
        } else {
            return pageNo - 1;
        }
    }

    public PageUtils(){}

    public PageUtils(long pageNo){
        this.pageNo=pageNo;
    }

    public PageUtils(long pageNo,long pageSize){
        this.pageNo=pageNo;
        this.pageSize = pageSize;
    }

    public List<?> getSumData() {
        return sumData;
    }
    public void setSumData(List<?> sumData) {
        this.sumData = sumData;
    }

}

